package org.cmisarium.xcmis.fsprovider;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xcmis.spi.BaseItemsIterator;
import org.xcmis.spi.ConstraintException;
import org.xcmis.spi.ContentStream;
import org.xcmis.spi.DocumentData;
import org.xcmis.spi.FolderData;
import org.xcmis.spi.ItemsIterator;
import org.xcmis.spi.NameConstraintViolationException;
import org.xcmis.spi.NotSupportedException;
import org.xcmis.spi.ObjectData;
import org.xcmis.spi.StorageException;
import org.xcmis.spi.TypeManager;

public class FolderDataImpl extends ObjectDataImpl implements FolderData
{

   public FolderDataImpl(File file, String rootPath, TypeManager typeManager)
   {
      super(file, rootPath, typeManager);

   }

   public ItemsIterator<ObjectData> getChildren(String orderBy)
   {

      File[] list = file.listFiles();

      List<ObjectData> children = new ArrayList<ObjectData>(list.length);

      for (File child : list)
      {

         ObjectData object;
         if (child.isDirectory())
            object = new FolderDataImpl(child, rootPath, typeManager);
         else
            object = new DocumentDataImpl(child, rootPath, typeManager);

         children.add(object);
      }

      return new BaseItemsIterator<ObjectData>(children);

   }

   public String getPath()
   {

      return "/" + path(file.getAbsolutePath(), rootPath);
   }

   public boolean hasChildren()
   {

      return file.list().length > 0;
   }

   public boolean isAllowedChildType(String typeId)
   {

      return true;
   }

   public boolean isRoot()
   {

      return this.file.getAbsolutePath().equals(this.rootPath);
   }

   public FolderData createFolder(String name) throws NameConstraintViolationException, StorageException
   {
      File newFile = new File(file, name);
      if (newFile.exists())
         throw new NameConstraintViolationException("Object with name '" + name + "' already exists");

      if (!newFile.mkdir())
         throw new StorageException("Can not create folder '" + name + "'");
      return new FolderDataImpl(newFile, rootPath, typeManager);
   }

   public DocumentData createDocument(String name, ContentStream content) throws NameConstraintViolationException,
      StorageException, IOException
   {
      File newFile = new File(file, name);
      if (newFile.exists())
         throw new NameConstraintViolationException("Object with name '" + name + "' already exists");

      try
      {
         if (!newFile.createNewFile())
            throw new StorageException("Can not create document '" + name + "'");
      }
      catch (IOException e)
      {
         throw new StorageException(e);
      }
      DocumentDataImpl doc = new DocumentDataImpl(newFile, rootPath, typeManager);
      if (content != null)
         doc.writeContent(content);
      return doc;
   }

   public void addObject(ObjectData object) throws ConstraintException
   {
      throw new NotSupportedException();
   }

   public void removeObject(ObjectData object)
   {
      throw new NotSupportedException();
   }

}
